package com.bawei.hgshop.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.bawei.hgshop.entity.MsgResult;
import com.bawei.hgshop.entity.Sku;
import com.bawei.hgshop.entity.SkuVo;
import com.bawei.hgshop.entity.Spec;
import com.bawei.hgshop.entity.SpecOption;
import com.bawei.hgshop.entity.Spu;
import com.bawei.hgshop.service.SkuService;
import com.bawei.hgshop.service.SpecService;
import com.bawei.hgshop.service.SpuService;
import com.bawei.hgshop.utils.HgFile;
import com.github.pagehelper.PageInfo;

@RequestMapping("sku")
@Controller
public class SkuController {

	@Reference
	SpuService spuService;
	
	@Reference
	SkuService skuService;
	
	/**
	 * 规格
	 */
	@Reference
	SpecService specService;
	
	@RequestMapping("toadd")
	public String toAdd(HttpServletRequest request,int spuId) {
		// 获取spu信息，在这个spu下添加sku
		Spu spu = spuService.getById(spuId);
		request.setAttribute("spu", spu);
		
		// 获取所有的规格
		List<Spec> specs  = specService.listAll();
		request.setAttribute("specs", specs);
		
		return "sku/add";
	}
	
	/**
	 * 进入修改页面
	 * @param request
	 * @param spuId
	 * @return
	 */
	@RequestMapping("toupdate")
	public String toupdate(HttpServletRequest request,int id) {
		
		// 获取sku
		Sku sku = skuService.findById(id);
		
		// 获取所有的规格
		List<Spec> specs  = specService.listAll();
		request.setAttribute("specs", specs);
		
		request.setAttribute("sku", sku);
		
		return "sku/update";
	}
	
	
	@RequestMapping("list")
	public String list(HttpServletRequest request,SkuVo skuVo) {
		PageInfo<Sku> pageInfo=  skuService.list(skuVo);
		request.setAttribute("pageInfo", pageInfo);
		return "sku/list";
	} 
	
	
	
	
	/**
	 *  添加sku
	 * @param spu<>
	 * @param file
	 * @return
	 */
	@RequestMapping("add")
	@ResponseBody
	public MsgResult add(Sku sku,MultipartFile imageFile, MultipartFile cartThumbnailFile) {
		
		// 上传图片
		String filePath = HgFile.upload(imageFile);
		// 设置图片路径
		sku.setImage(filePath);
		
		// 上传图片
				 filePath = HgFile.upload(cartThumbnailFile);
				// 设置图片路径
				sku.setCartThumbnail(filePath);
		
		List<SpecOption> options = sku.getSpecOptions();
		//数据进行清洗
		for (int i = options.size()-1; i >=0 ; i--) {
			SpecOption option = options.get(i);
			// 如果这个数据不合法则删除
			if(option.getSpecId()==0 || option.getId()==0)
				options.remove(i);
		}
		
		
		int n = skuService.add(sku);
		
		if(n>0)
			return new MsgResult(0, "", null);
		
		return new MsgResult(1, "添加失败", null);
		
	}
	
	
	
	

	/**
	 *  添加sku
	 * @param spu<>
	 * @param file
	 * @return
	 */
	@RequestMapping("update")
	@ResponseBody
	public MsgResult update(Sku sku,MultipartFile imageFile, 
			MultipartFile cartThumbnailFile) {
		
		// 上传图片
		String filePath = HgFile.upload(imageFile);
		// 设置图片路径
		sku.setImage(filePath);
		
		// 上传图片
				 filePath = HgFile.upload(cartThumbnailFile);
				// 设置图片路径
				sku.setCartThumbnail(filePath);
		
		List<SpecOption> options = sku.getSpecOptions();
		//数据进行清洗
		for (int i = options.size()-1; i >=0 ; i--) {
			SpecOption option = options.get(i);
			// 如果这个数据不合法则删除
			if(option.getSpecId()==0 || option.getId()==0)
				options.remove(i);
		}
		
		
		int n = skuService.update(sku);
		
		if(n>0)
			return new MsgResult(0, "", null);
		
		return new MsgResult(1, "修改失败", null);
		
	}
}
